﻿using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using D.UtilCore;
using CozeDomain.IServices;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Authorization;
using D.UtilFreesqlOrm;
using CozeDto.ConversationDtos;

namespace CozeWebApi
{
    [Route("/api/[controller]/[action]")]
    public class ConversationController : BaseApiController
    {

        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly IConversationService _conversationService;

        /// <summary>
        /// 构造注入
        /// </summary>
        public ConversationController(IWebHostEnvironment hostingEnvironment, IConversationService conversationService)
        {
            _hostingEnvironment = hostingEnvironment;
            _conversationService = conversationService;
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqAddConversation"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddConversation  reqAddConversation)
        {
            reqAddConversation.CreateUser = Account;
            return await _conversationService.AddConversationAsync(reqAddConversation);
        }

        /// <summary>
        /// 删除，多个逗号隔开
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        [UnitOfWork]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _conversationService.DeleteConversationAsync(idsArray.ToList());
        } 

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepConversationInfo>>> Pages([FromBody] ReqQueryConversationPage reqQuery)
        {
            return await _conversationService.QueryConversationPagesAsync(reqQuery);
        }

    }
}
